package com.supor.aiot.food.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.supor.aiot.common.ui.view.DotTextView;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.ChildList;
import com.supor.aiot.food.bean.ChildListBean;
import com.supor.aiot.food.bean.Children;
import com.supor.aiot.food.bean.RecipeDeviceBean;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ClassifyRightAdapter extends RecyclerView.Adapter<ClassifyRightAdapter.MyRightViewHolder> {
    private Context mContext;
    private List<ChildListBean> mDataBeanList;
    private List<RecipeDeviceBean> mDeviceList;
    private ClassifyContentAdapter mMyGoodsRightAdapter;
    private ClassifyDeviceAdapter classifyDeviceAdapter;
    private ClassifyTopAdapter classifyTopAdapter;
    private OnClassifyItemClickListener listener;
    public ClassifyRightAdapter(Context context, List<ChildListBean> dataBeans,List<RecipeDeviceBean> deviceList,OnClassifyItemClickListener listener) {
        mContext = context;
        mDataBeanList = dataBeans;
        this.listener=listener;
        this.mDeviceList = deviceList;
    }
 
    //更新适配器
    public void update(List<ChildListBean> dataBeans){
        this.mDataBeanList = dataBeans;
        notifyDataSetChanged();
    }
 
    @NonNull
    @Override
    public MyRightViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_classify_content, viewGroup, false);
        return new MyRightViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull MyRightViewHolder myRightViewHolder, int i) {
        ViewGroup.LayoutParams layoutParams = myRightViewHolder.itemView.getLayoutParams();
        if (i == mDataBeanList.size() - 1){
            layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
            myRightViewHolder.mClassifyBottom.setVisibility(View.VISIBLE);
        } else {
            layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            myRightViewHolder.mClassifyBottom.setVisibility(View.GONE);
        }

        myRightViewHolder.mTextViewTitle.setText(mDataBeanList.get(i).getClassificationName());
        if (mDataBeanList.get(i).isClicked()) {
            //myRightViewHolder.mTextViewTitle.setDotVisible(true);
            myRightViewHolder.mTextViewTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            //myRightViewHolder.rlClassifyOuter.setVisibility(View.GONE);
        } else {
            //myRightViewHolder.mTextViewTitle.setDotVisible(false);
            myRightViewHolder.mTextViewTitle.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            //myRightViewHolder.rlClassifyOuter.setVisibility(View.VISIBLE);
        }

        ChildListBean bean = mDataBeanList.get(i);
        List<ChildList> list = mDataBeanList.get(i).getChildList();
        if (bean.getClassificationName().equals(mContext.getResources().getString(R.string.recipe_hot))){
            classifyTopAdapter =  new ClassifyTopAdapter(list, mContext, position -> listener.OnTopClicked(i,position));
            myRightViewHolder.mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));//网格布局
            myRightViewHolder.mRecyclerView.setAdapter(classifyTopAdapter);
        }else if (bean.getClassificationName().contains(mContext.getResources().getString(R.string.recipe_devices))){
            List<Children> children = new ArrayList<>();
            if (mDeviceList.size()>0){
                for (RecipeDeviceBean deviceBean:mDeviceList){
                    // children1.setModelPicture(deviceBean.getModelPicture());
                    children.addAll(deviceBean.getChildren());
                }
                classifyDeviceAdapter =  new ClassifyDeviceAdapter(children,mContext, position -> {listener.OnDeviceClicked(children.get(position).getModelName(),children.get(position).getCrmModelName());});
                myRightViewHolder.mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));//网格布局
                myRightViewHolder.mRecyclerView.setAdapter(classifyDeviceAdapter);
            }
        }else {
            //添加子布局适配器
            if (list!=null){
                mMyGoodsRightAdapter = new ClassifyContentAdapter(list, mContext, position -> {listener.OnClassifyClicked(i,position);});
                myRightViewHolder.mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3));//网格布局
                myRightViewHolder.mRecyclerView.setAdapter(mMyGoodsRightAdapter);
            }
        }

    }
 
    @Override
    public int getItemCount() {
        return mDataBeanList == null ? 0 : mDataBeanList.size();
    }
 
    //自定义viewholder
    class MyRightViewHolder extends RecyclerView.ViewHolder{
        DotTextView mTextViewTitle;
        RecyclerView mRecyclerView;
        RelativeLayout rlClassifyOuter;
        View mClassifyBottom;
        public MyRightViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextViewTitle = itemView.findViewById(R.id.tv_classify_title);
            mRecyclerView = itemView.findViewById(R.id.rv_classify_types);
            rlClassifyOuter = itemView.findViewById(R.id.rl_classify_outer);
            mClassifyBottom = itemView.findViewById(R.id.classify_bottom_view);
        }
    }


    public interface OnClassifyItemClickListener{
        void OnTopClicked(int origin ,int position);
        void OnDeviceClicked(String deviceModel,String deviceName);
        void OnClassifyClicked(int origin,int position);
    }
}